<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>原生的Ajax操作</h1>
    <button>加载Ajax数据</button>
    <script>
        // 原生Ajax获取学生信息并输出

        const btn = document.querySelector("button");


        // 绑定事件
        btn.onclick = function(){
            // 1. 创建XHR对象
            const xhr = new XMLHttpRequest();
            // 2. 打开连接
            xhr.responseType = "json";
            xhr.open("GET","http://localhost:3000/stu");
            // 3. 绑定状态改变的监听, 并在异步回调中读取响应数据
            xhr.onreadystatechange = function(){
                //判断状态
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                        let data = xhr.response;
                        console.log(data);
                    }
                }
            }
            // 4. 发送请求
            xhr.send();
    }
    </script>
</body>
</html>